package jp.co.lab1;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	private static final String TAG = "Memo Book";
	private SharedPreferences mPrefs; 
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.edit);
        
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        String content = mPrefs.getString("content", "");
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy/MM/dd (E)");
        Calendar cal = Calendar.getInstance();
        String date = fmt.format(cal.getTime());
        TextView txtsubject = (TextView) findViewById(R.id.TextView01);
        EditText txtcontent = (EditText) findViewById(R.id.EditText01);
        txtsubject.setText(date);
        txtcontent.setText(content);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    public void onClickButton(View view) {
    	switch(view.getId()) {
    	case R.id.Button01: {
    		AlertDialog.Builder dlg = new AlertDialog.Builder(this);
    		dlg.setTitle(getString(R.string.lbSaveTitle));
    		dlg.setPositiveButton(getString(R.string.lbYes), new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					EditText txtcontent = (EditText) findViewById(R.id.EditText01);
					String content = txtcontent.getText().toString();
					Log.d(TAG, "content=" + content);
					Editor editor = mPrefs.edit();
					editor.putString("content", content);
					editor.commit();
					finish();
				}
			});
    		dlg.setNegativeButton(getString(R.string.lbNo), new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					
				}
			});
    		dlg.show();
    		}break;
    	case R.id.Button02:{
    		finish();
    		}break;
    	} 
    }
}